package com.loanapp.server.market.provider.modular.home.service.impl;

import com.loanapp.base.database.common.base.SqlMapper;
import com.loanapp.server.market.api.platform.entity.CmsComment;
import com.loanapp.server.market.provider.modular.home.service.InformationService;
import com.loanapp.server.market.provider.modular.platform.mapper.CmsCommentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Desc: 资讯 实现类
 * Author: dushuang
 * Date: Create in 2017/8/29
 */
@Service
public class InformationServiceImpl implements InformationService {

    @Autowired
    private SqlMapper sqlMapper;

    @Autowired
    private CmsCommentMapper commentMapper;

    @Override
    public List<Map<String, Object>> getInformationList(String cmsChannel, int[] page) {
        int pageNo = page[0];
        int pageSize = page[1];
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT ");
        sql.append("a.id, ");
        sql.append("a.url, ");
        sql.append("a.title, ");
        sql.append("a.create_time,");
        sql.append("a.discuss_count, ");
        sql.append("a.title_img as img ");
        sql.append("FROM cms_content a ");
        sql.append("WHERE 1=1 ");
        sql.append("AND a.`status` = 1 ");      // 内容是启用状态
        sql.append("AND a.channel_id IN ("+cmsChannel+") ");
        sql.append("ORDER BY a.create_time DESC ");
        sql.append("LIMIT " + (pageNo - 1) * pageSize + "," + pageSize + " ");
        return sqlMapper.selectList(sql.toString());
    }

    @Override
    public List<Map<String, Object>> getDiscussList(String informationId, int[] page) {
        int pageNo = page[0];
        int pageSize = page[1];
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT ");
        sql.append("a.id, ");
        sql.append("a.cms_id AS informationId, ");
        sql.append("a.content, ");
        sql.append("a.discuss_user, ");
        sql.append("a.create_time ");
        sql.append("FROM cms_comment a ");
        sql.append("WHERE 1=1 ");
        sql.append("AND a.show_status = 1 ");      // 内容是启用状态
        sql.append("AND a.cms_id = '"+informationId+"' ");
        sql.append("LIMIT " + (pageNo - 1) * pageSize + "," + pageSize + " ");
        return sqlMapper.selectList(sql.toString());
    }

    @Override
    public Integer saveCmsDiscuss(CmsComment cmsComment) {
        int result = commentMapper.insert(cmsComment);
        sqlMapper.update("update cms_content set discuss_count= discuss_count+1 where id='"+cmsComment.getCmsId()+"'");
        return result > 0 ? result : 0;
    }

    @Override
    public Integer deleteCmsDiscuss(String discussId, String informationId) {
        int result = sqlMapper.update("update cms_comment set show_status = 2 where id = '"+discussId+"'");
        sqlMapper.update("update cms_content set discuss_count= discuss_count-1 where id='"+informationId+"'");
        return result > 0 ? result : 0;
    }

    @Override
    public List<Map<String, Object>> getCustomInformationVisit(int[] page, String visitType, Long user) {
        StringBuilder sql = new StringBuilder();
        int pageNo = page[0];
        int pageSize = page[1];
        if ("1".equals(visitType)) {
            // 资讯
            sql.append("SELECT ");
            sql.append("a.id AS visitId, ");
            sql.append("arg0.id AS informationId, ");
            sql.append("arg0.title, ");
            sql.append("arg0.title_img AS img, ");
            sql.append("CASE ");
            sql.append("WHEN LENGTH(arg0.detail)>0 AND LENGTH(arg0.detail)<28 ");
            sql.append("THEN arg0.detail ");
            sql.append("WHEN LENGTH(arg0.detail)>=28 ");
            sql.append("THEN CONCAT(SUBSTR(arg0.detail,1,25),'...') ");
            sql.append("END AS detail, ");
            sql.append("arg0.create_time ");
            sql.append("FROM customer_information_visit a ");
            sql.append("LEFT JOIN cms_content arg0 ON a.information_id = arg0.id ");
            sql.append("WHERE 1=1 ");
            sql.append("AND arg0.status = 1 ");     // 启用状态
        }else if("2".equals(visitType)){
            // 帖子
            sql.append("SELECT ");
            sql.append("a.id AS visitId, ");
            sql.append("arg0.id AS informationId, ");
            sql.append("arg0.title, ");
            sql.append("arg0.img_url AS img, ");
            sql.append("CASE ");
            sql.append("WHEN LENGTH(arg0.content)>0 AND LENGTH(arg0.content)<28 ");
            sql.append("THEN arg0.content ");
            sql.append("WHEN LENGTH(arg0.content)>=28 ");
            sql.append("THEN CONCAT(SUBSTR(arg0.content,1,25),'...') ");
            sql.append("END AS detail, ");
            sql.append("arg0.create_time ");
            sql.append("FROM customer_information_visit a ");
            sql.append("LEFT JOIN bbs_post_base arg0 ON a.information_id = arg0.id ");
            sql.append("WHERE 1=1 ");
        }
        sql.append("AND a.visit_type = '" + visitType + "' ");
        sql.append("AND a.user_id = '" + user + "' ");
        sql.append("ORDER BY a.create_time DESC ");
        sql.append("LIMIT " + (pageNo - 1) * pageSize + "," + pageSize + " ");
        return sqlMapper.selectList(sql.toString());
    }

    @Override
    public void deleteInformationVisit(String visitType, String idStr, Long user) {
        sqlMapper.delete("DELETE FROM customer_information_visit WHERE visit_type = '" + visitType + "' AND user_id = '" + user + "' AND id IN(" + idStr + ") ");
    }

    @Override
    public void insertInformationVisit(String informationId, String visitType, Long user) {
        sqlMapper.delete("DELETE FROM customer_information_visit WHERE visit_type = '"+visitType+"' AND user_id = '"+user+"' AND information_id = '"+informationId+"' ");
        sqlMapper.insert("INSERT INTO customer_information_visit(information_id,user_id,visit_type,create_time) VALUES('"+informationId+"','"+user+"','"+visitType+"',NOW())");
    }

    public List<Map<String, Object>> getInformationBranchList(String cmsChannel, int[] page) {
        int pageNo = page[0];
        int pageSize = page[1];
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT ");
        sql.append("a.id, ");
        sql.append("a.url, ");
        sql.append("a.title, ");
        sql.append("b.`name` AS author, ");
        sql.append("CASE ");
        sql.append("WHEN LENGTH(detail)>0 AND LENGTH(detail)<28 ");
        sql.append("THEN detail ");
        sql.append("WHEN LENGTH(detail)>=28 ");
        sql.append("THEN CONCAT(SUBSTR(detail,1,25),'...') ");
        sql.append("END AS detail, ");
        sql.append("a.create_time,");
        sql.append("a.discuss_count, ");
        sql.append("a.title_img as img ");
        sql.append("FROM cms_content a ");
        sql.append("LEFT JOIN `user` b ON a.author = b.id ");
        sql.append("WHERE 1=1 ");
        sql.append("AND a.`status` = 1 ");      // 内容是启用状态
        sql.append("AND a.channel_id = '"+cmsChannel+"' ");
        sql.append("LIMIT " + (pageNo - 1) * pageSize + "," + pageSize + " ");
        return sqlMapper.selectList(sql.toString());
    }
}
